/**
 * 
 */
package com.jdy.haoduoaiteacher.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TextView.BufferType;

import com.jdy.haoduoaiteacher.MyTeacherApp;
import com.jdy.haoduoaiteacher.intface.CommentDelegate;
import com.jdy.haoduoaiteacher.model.ChildrenDiscussionItem;
import com.jdy.haoduoaiteacher.model.LeUser;
import com.jdy.haoduoaiteacher.model.Member;
import com.jdy.haoduoaiteacher.util.Tools;
import com.jdy.haoduoaiteacher.view.ListViewForScrollView;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

/**
 * @author zp 2016-11-30 下午4:47:48
 */
public class DiscussionAdapter extends BaseAdapter {

  private final Context mContext;

  private final ArrayList<ChildrenDiscussionItem> mDiscussionList;

  private final LayoutInflater mInflater;

  private final SparseArray<View> mSpArray;

  private CommentDelegate mCommentDelegate;

  private final Member hostMember;

  public DiscussionAdapter(Context ctx, ArrayList<ChildrenDiscussionItem> list,
      Member host) {

    this.mContext = ctx;

    this.mDiscussionList = list;

    if (this.mDiscussionList != null) {
      for (ChildrenDiscussionItem cdi : this.mDiscussionList) {
        cdi.initialize();
      }
    }

    this.hostMember = host;

    this.mInflater = LayoutInflater.from(mContext);

    this.mSpArray = new SparseArray<View>();

  }

  public void setCommentDelegate(CommentDelegate cd) {
    this.mCommentDelegate = cd;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getCount()
   */
  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    if (mDiscussionList != null)
      return mDiscussionList.size();
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItem(int)
   */
  @Override
  public ChildrenDiscussionItem getItem(int position) {
    // TODO Auto-generated method stub
    if (mDiscussionList != null)
      return mDiscussionList.get(position);
    else
      return null;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getItemId(int)
   */
  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    if (mDiscussionList != null)
      return position;
    else
      return 0;
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.widget.Adapter#getView(int, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
    convertView = mSpArray.get(position);
    if (convertView == null) {
      final ChildrenDiscussionItem di = mDiscussionList.get(position);
      holder = new ViewHolder();
      LeUser lu = MyTeacherApp.getInstance().getUserTeacher();
      String loginMemberId = lu.id;
      convertView = mInflater.inflate(R.layout.discussion_comment_item_layout,
          null);
      holder.titleContentTv = (TextView) convertView
          .findViewById(R.id.title_content_tv);

      holder.replyTv = (TextView) convertView.findViewById(R.id.reply_btn);

      holder.replyTv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          // TODO Auto-generated method stub
          if (mCommentDelegate != null)
            mCommentDelegate.publishCommentDelegate(di.id);
        }
      });

      SpannableStringBuilder builder = new SpannableStringBuilder();

      if (di.member_id.equals(hostMember.id)
          && Tools.isNotEmpty(di.parentMember)
          && !di.parentMember.id.equals(hostMember.id)) {

        String self = hostMember.nickname;
        SpannableString selfSpannable = new SpannableString(self);
        selfSpannable.setSpan(
            new ForegroundColorSpan(Color.argb(255, 106, 197, 253)), 0,
            self.length(), 0);
        builder.append(selfSpannable);

        String REPLY = "回复";
        SpannableString replySpannable = new SpannableString(REPLY);
        replySpannable.setSpan(
            new ForegroundColorSpan(Color.argb(255, 153, 153, 153)), 0,
            REPLY.length(), 0);
        builder.append(replySpannable);

        String target = di.parentMember.nickname + ":";

        SpannableString targetSpannable = new SpannableString(target);

        targetSpannable.setSpan(
            new ForegroundColorSpan(Color.argb(255, 106, 197, 253)), 0,
            target.length(), 0);

        builder.append(targetSpannable);

        String content = di.comment;

        SpannableString contentSpannable = new SpannableString(content);

        contentSpannable.setSpan(
            new ForegroundColorSpan(Color.argb(255, 153, 153, 153)), 0,
            content.length(), 0);
        builder.append(contentSpannable);

      } else {

        String self = di.member.nickname + ":";

        SpannableString selfSpannable = new SpannableString(self);

        selfSpannable.setSpan(
            new ForegroundColorSpan(Color.argb(255, 106, 197, 253)), 0,
            self.length(), 0);

        builder.append(selfSpannable);

        String content = di.comment;

        SpannableString contentSpannable = new SpannableString(content);

        contentSpannable.setSpan(
            new ForegroundColorSpan(Color.argb(255, 153, 153, 153)), 0,
            content.length(), 0);
        builder.append(contentSpannable);

      }

      holder.titleContentTv.setText(builder, BufferType.SPANNABLE);

      if (loginMemberId.equals(hostMember.id)
          && !di.member_id.equals(hostMember.id)) {
        holder.replyTv.setVisibility(View.VISIBLE);
      }

      holder.replyCommentLV = (ListViewForScrollView) convertView
          .findViewById(R.id.reply_comment_lv);

      if (Tools.isNotEmpty(di.children_comments)) {
        holder.replyCommentLV.setAdapter(new DiscussionAdapter(mContext,
            di.children_comments, hostMember));
      } else {
        holder.replyCommentLV.setVisibility(View.GONE);
      }

      convertView.setTag(holder);
      mSpArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    return convertView;
  }

  private class ViewHolder {
    TextView titleContentTv;

    ListViewForScrollView replyCommentLV;

    TextView replyTv;
  }

}
